import { getUrlParams } from './utils';

// mock tableListDataSource
const tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
  tableListDataSource.push({
    record_registerTime: '2018-12-01 00:00:00',
    record_addTime: '2018-12-01 00:00:00',
    record_appMgr: (i % 4) + 1,
    record_baseIdent: i % 2,
    record_idIdent: i % 2,
    record_type: (i % 3) + 1,
    record_num: i,
    record_apply: i,
    record_status: i,
    record_userd: i,
  });
}

export function getRecommendRecord(req, res, u) {
  let url = u;
  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    url = req.url; // eslint-disable-line
  }

  const params = getUrlParams(url);

  let dataSource = [...tableListDataSource];

  try {
    for (const key in params) {
      const value = params[key];
      dataSource = dataSource.filter((data) => {
        return data[key] == value;
      });
    }
  } catch (e) {
    console.log(e);
  }

  let pageSize = 10;
  if (params.pageSize) {
    pageSize = params.pageSize * 1;
  }

  const result = {
    list: dataSource,
    pagination: {
      total: dataSource.length,
      pageSize,
      current: parseInt(params.currentPage, 10) || 1,
    },
  };

  if (res && res.json) {
    res.json(result);
  } else {
    return result;
  }
}

export default {
  getRecommendRecord,
};
